﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using HundredMilesSoftware.UltraID3Lib;

namespace Apollo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MediaPlayer mediaPlayer;
        private List<Track> playlist;

        public MainWindow()
        {
            this.InitializeComponent();
            
            mediaPlayer = new MediaPlayer();
            playlist = new List<Track>();
        }

        private void testPlayButton_Click(object sender, RoutedEventArgs e)
        {
            PlaySelectedTrack();
        }

        private void trackList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            PlaySelectedTrack();
        }

        private void PlaySelectedTrack()
        {
            Track track = trackList.SelectedItem as Track;
            if (track == null) return;

            mediaPlayer.Open(new Uri(track.Filename, UriKind.Absolute));
            mediaPlayer.Play();

            UltraID3 id3Tags = track.ID3Tags;
            trackName.Content = id3Tags.Title;
            artistName.Content = id3Tags.Artist;
            albumName.Content = id3Tags.Album;

            var found = from t in playlist
                        where t.ID3Tags.Album == id3Tags.Album
                        select t;
            currentAlbumList.ItemsSource = found;
        }

        private void loadFilesButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Filter = "Audio Files (*.mp3, *.wma, *.wav)|*.mp3;*.wma;*.wav|All Files|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] fileNames = ofd.FileNames;
                string[] safeFileNames = ofd.SafeFileNames;
                foreach (string fileName in fileNames)
                {
                    Track track = new Track(fileName);
                    playlist.Add(track);
                    trackList.Items.Add(track);
                }
            }
        }
    }
}